from django.contrib import admin
from blog.models import Category, Article, Nav, Carousel, Tag, Recommend

# Register your models here.


@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'author', 'category', 'pub_time', 'update_time', 'is_top')
    exclude = ('author', 'view_count', )

    def save_model(self, request, obj, form, change):
        obj.author = request.user
        return super().save_model(request, obj, form, change)


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', )


@admin.register(Nav)
class NavAdmin(admin.ModelAdmin):
    search_fields = ('name', )
    list_display = ('name', 'url', 'create_time', 'rank')
    list_filter = ('create_time', )
    fields = ('name', 'url', 'rank')


@admin.register(Carousel)
class CarouselAdmin(admin.ModelAdmin):
    search_fields = ('article',)
    list_display = ('article', 'img', 'create_time',)
    list_filter = ('create_time',)
    fields = ( 'article', 'img', )


@admin.register(Recommend)
class RecommendAdmin(admin.ModelAdmin):
    search_fields = ('article',)
    list_display = ('article', 'rank',)


admin.site.register(Tag)

